package javaLanguage.javaMultithread;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

public class asychronizedThread {
    private static volatile Integer count = Integer.valueOf(0);
    public static void main(String[] args) {
        FutureTask<Integer> futureTask = new FutureTask<>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                for(int i = 0; i < 10; i ++) {
                    try {
                        Thread.sleep(500);
                    } catch (Exception e){
                        e.printStackTrace();
                    }

                    count += 1;
                }
                return count;
            }
        });

        System.out.printf("主线程开始......\n");
        new Thread(futureTask).start();
        while (true){
            try {
                Thread.sleep(1000);
                System.out.printf("目前任务进行中，值为%d\n", count);
            } catch (Exception e){
                e.printStackTrace();
            }

            if(futureTask.isDone()) {
                System.out.printf("线程执行完毕！即将退出程序......\n");
                break;
            }
        }
    }
}
